using System;
using System.Xml.Serialization;
using System.Linq;
using System.Collections.Generic;

namespace ElfBinary
{
	/// <summary>
	/// This class contains information which directs the implementation
	/// on how to read and parse the objects contained in an ELF binary.
	/// </summary>
	[Serializable]
	public class ElfIdentification
	{
		public ElfIdentification ()
		{
		}

		public void Read(System.IO.Stream s)
		{
			s.Read (magicnumber, 0, 4);
			s.Read (fileclass, 0, 1);
			s.Read (dataencoding, 0, 1);
			s.Read (fileversion, 0, 1);
			s.Read (osabi, 0, 1);
			s.Read (abiversion, 0, 1);
			s.Read (padding, 0, 6);
			s.Read (eidentsize, 0, 1);
		}

		#region fields
		private byte[] magicnumber = new byte[4];
		private byte[] fileclass = new byte[1];
		private byte[] dataencoding = new byte[1];
		private byte[] fileversion = new byte[1];
		private byte[] osabi = new byte[1];
		private byte[] abiversion = new byte[1];
		private byte[] padding  = new byte[6];
		private byte[] eidentsize = new byte[1];
		#endregion

		[XmlElement]
		public string MagicNumber {
			 get { 

				string s = string.Empty;

				magicnumber.ToList ().ForEach (b => {
					s+=(char)b;
				});
				return s;
			}
			set { 

				List<byte> b = value.ToCharArray ().Select (o => (byte)o).ToList ();

				magicnumber = 
					b.ToArray(); 
			}
		}

		[XmlElement]
		public ElfClass FileClass { 
			get { return (ElfClass)fileclass[0]; }
			set { fileclass [0] = (byte)value; }
		}

		[XmlElement]
		public ElfData DataClass { 
			get { return (ElfData) dataencoding[0]; }
			set { dataencoding [0] = (byte)value; }
		}

		[XmlElement]
		public byte FileVersion {
			 get { return fileversion[0]; }
			set { fileversion [0] = (byte)value; }
		}

		[XmlElement]
		public ElfABI OSABI { 
			get { return (ElfABI) osabi[0]; }
			set { osabi [0] = (byte)value; }
		}

		[XmlElement]
		public byte ABIVersion { 
			get { return abiversion[0]; }
			set { abiversion [0] = value; }
		}

		[XmlIgnore]
		public byte[] Padding { get { return padding; }}

		[XmlElement]
		public byte IdentSize {
			 get { return eidentsize[0]; }
			set { eidentsize [0] = value; }
		}


		}
}

